import { result } from 'lodash';
/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-06-23 12:09:31
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-11-25 15:32:59
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import { MessageService } from '@abp/message/message.service';
import { SysCodeServiceProxy, SysCodeDto } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
@Component({
    selector: 'userCodeEditOrAdd',
    templateUrl: './user-code-edit-or-add.component.html',
    styleUrls: ['./user-code-edit-or-add.component.less']
})
export class UserCodeEditOrAddComponent extends AppComponentBase {
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() editData: any;  //左侧行数据
    @Input() sonData: any;  //左侧行数据
    @Input() title: string;  //标题
    saving = false;
    active = false;
    val = true;
    form = { sonName: '', remark: '' };
    saveFlag = true;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _sysCodeServiceProxy: SysCodeServiceProxy,
    ) {
        super(injector);
    }

    ngOnInit() {
    }
    //保存
    save(): void {
        //获取实体
        let input = new SysCodeDto();
        if (this.val) {
            input.isUsed = 1;
        } else {
            input.isUsed = 0;
        }
        if (this.title == '修改') {
            input.id = this.sonData.id;
        }
        input.description = this.form.sonName;
        input.pPtr = this.editData.id;
        input.codeId = this.editData.ccodeId;
        input.remark = this.form.remark;
        //将form表单赋值给input的bopdesc下
        if (this.saveFlag) {
            this.saveFlag = false;
            this._sysCodeServiceProxy.createOrUpdateCode(input)
                .pipe(finalize(() => { this.saving = false; }))
                .subscribe((result) => {
                    this.saveFlag = true;
                    if (result.code == 0) {
                        this._messageService.success('保存成功');
                        this.close();
                        this.modalSave.emit();
                    } else {
                        this._messageService.error(result.message)
                    }
                });
        }

    }
    //打开弹窗
    onShown(): void {
        if (this.title == '修改') {
            this.form.sonName = this.sonData.description;
            if (this.sonData.isUsed == 1) {
                this.val = true;
            } else {
                this.val = false;
            }
            this.form.remark = this.sonData.remark;
        }


    }
    //查看新增任务
    show(): void {
        if (this.title == '新增') {
            this.form.remark = undefined;
            this.form.sonName = undefined;
        }
        this.active = true;
        this.modal.show();
    }
    //关闭事件
    close(): void {
        //this.sonData = undefined;
        this.saveFlag = true;
        this.val = true;
        //清空form表单
        this.form.remark = undefined;
        this.form.sonName = undefined;
        this.active = false;
        this.modal.hide();
    }

}
